package attendance

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/attendance"
	attendanceReq "github.com/flipped-aurora/gin-vue-admin/server/model/attendance/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type SignApi struct {
}

var signService = service.ServiceGroupApp.AttendanceServiceGroup.SignService

// CreateSign 创建签到管理
// @Tags Sign
// @Summary 创建签到管理
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body attendance.Sign true "创建签到管理"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /sign/createSign [post]
func (signApi *SignApi) CreateSign(c *gin.Context) {
	var sign attendance.Sign
	err := c.ShouldBindJSON(&sign)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	sign.BelongTo = utils.GetBelongTo(c)
	if err := signService.CreateSign(&sign); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteSign 删除签到管理
// @Tags Sign
// @Summary 删除签到管理
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body attendance.Sign true "删除签到管理"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /sign/deleteSign [delete]
func (signApi *SignApi) DeleteSign(c *gin.Context) {
	ID := c.Query("ID")
	if err := signService.DeleteSign(ID); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteSignByIds 批量删除签到管理
// @Tags Sign
// @Summary 批量删除签到管理
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /sign/deleteSignByIds [delete]
func (signApi *SignApi) DeleteSignByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	if err := signService.DeleteSignByIds(IDs); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateSign 更新签到管理
// @Tags Sign
// @Summary 更新签到管理
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body attendance.Sign true "更新签到管理"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /sign/updateSign [put]
func (signApi *SignApi) UpdateSign(c *gin.Context) {
	var sign attendance.Sign
	err := c.ShouldBindJSON(&sign)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := signService.UpdateSign(sign); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindSign 用id查询签到管理
// @Tags Sign
// @Summary 用id查询签到管理
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query attendance.Sign true "用id查询签到管理"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /sign/findSign [get]
func (signApi *SignApi) FindSign(c *gin.Context) {
	ID := c.Query("ID")
	if resign, err := signService.GetSign(ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"resign": resign}, c)
	}
}

// GetSignList 分页获取签到管理列表
// @Tags Sign
// @Summary 分页获取签到管理列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query attendanceReq.SignSearch true "分页获取签到管理列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /sign/getSignList [get]
func (signApi *SignApi) GetSignList(c *gin.Context) {
	var pageInfo attendanceReq.SignSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := signService.GetSignInfoList(pageInfo, utils.GetBelongTo(c)); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetSignPublic 不需要鉴权的签到管理接口
// @Tags Sign
// @Summary 不需要鉴权的签到管理接口
// @accept application/json
// @Produce application/json
// @Param data query attendanceReq.SignSearch true "分页获取签到管理列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /sign/getSignPublic [get]
func (signApi *SignApi) GetSignPublic(c *gin.Context) {
	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的签到管理接口信息",
	}, "获取成功", c)
}
